package element.synt;

import element.ast.AstNode;
import element.base.Synt;
import intermediate_representation.AssignRP;
import table.InterCodeTable;

//Exp → AddExp
public class Exp extends Synt {
    public AstNode toAst() {
        AddExp addExp = (AddExp) this.getNodes().get(0);
        return addExp.toAst();
    }

    public int calcu() {
        AddExp addExp = (AddExp) this.getNodes().get(0);
        return addExp.calcu();
    }

    public boolean isConst() {
        AddExp addExp = (AddExp) this.getNodes().get(0);
        return addExp.isConst();
    }

    @Override
    public void tointerRP() {
        if (this.isConst()) {
            int res = this.calcu();
            InterCodeTable.pushvar();
            String returns = InterCodeTable.nowVar;
            AssignRP assignRP = new AssignRP(returns, String.valueOf(res));
            InterCodeTable.addCode(assignRP);
        } else {
            AstNode astNode = this.toAst();
            astNode.tointerRP();
        }
//        AstNode astNode = this.toAst();
//        astNode.tointerRP();
    }

    public void tofuncRP() {
        AstNode astNode = this.toAst();
        astNode.tofuncRP();
    }
}
